﻿#region (c) 2009 Abdullin.com and Contributors

// Copyright (c) 2009 Abdullin.com and Contributors
// Site: http://abdullin.com/ncouch
// This code is released under the terms of the new BSD licence

#endregion

using System;
using System.Diagnostics;
using System.Linq;
using NCouch.Synthetic.Model;
using NUnit.Framework;

namespace NCouch.Synthetic
{
	[SetUpFixture]
	public sealed class GlobalSetup
	{
		const string ServerUrl = "http://localhost:5984";

		public static readonly CouchServer TestServer = CouchFactory.ConnectToServer(ServerUrl);
		public const string TestPrefix = "synth_";

		static GlobalSetup()
		{
			CouchFactory.Build(x => x
				.RegisterCSharpData<Task>().WithKey(t => t.TaskID));
		}


		public static CouchDatabase ConnectToDatabase(string name)
		{
			return CouchFactory.ConnectToDatabase(ServerUrl + "/" + name);
		}

		[SetUp]
		public void SetUp()
		{
			WipeDatabasesForSyntheticTests();
		}

		[TearDown]
		public void TearDown()
		{
			WipeDatabasesForSyntheticTests();
		}

		static void WipeDatabasesForSyntheticTests()
		{
			var testDatabases = TestServer
				.GetDatabaseNames()
				.Where(dn => dn.StartsWith(TestPrefix));

			foreach (var database in testDatabases)
			{
				TestServer.DeleteDatabase(database);
			}
		}
	}
}